﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using org.in2bits.MyXls;

namespace CommunitySystem.PageView
{
    public partial class BianZhiControl : UserControl
    {
        private EmpbasicService.EmpBasicServiceClient Client = new EmpbasicService.EmpBasicServiceClient();
        EmpbasicService.BianZhiModel RoleModel = new EmpbasicService.BianZhiModel();
        private int OrgID = 0;

        public ObservableCollection<EmpbasicService.BianZhiModel> BianZhiModels =
            new ObservableCollection<EmpbasicService.BianZhiModel>();
        public BianZhiControl()
        {
            InitializeComponent();
            Client.QuXiaoTongChouCompleted += new EventHandler<EmpbasicService.QuXiaoTongChouCompletedEventArgs>(Client_QuXiaoTongChouCompleted);
            Client.GetOrgForHRModelCompleted += new EventHandler<EmpbasicService.GetOrgForHRModelCompletedEventArgs>(Client_GetOrgForHRModelCompleted);
            Client.GetOrgForHRModelAsync(0);
        }

        
        #region 左右拖动的条
        private GridLength _lastColLeftLength;
        private GridLength _lastColRightLength;
        private void GridSpliteTo_ToLeft(object sender, EventArgs e)
        {
            //已经到最左边，直接返回
            if (colLeft.Width.Value == 0.0)
            {
                return;
            }

            //
            if (colRight.Width.Value == 0)
            {
                colLeft.Width = _lastColLeftLength;
                colRight.Width = _lastColRightLength;
            }
            else
            {
                _lastColLeftLength = colLeft.Width;
                _lastColRightLength = colRight.Width;

                colLeft.Width = new GridLength(0);
                colRight.Width = new GridLength(150, GridUnitType.Star);
            }
        }

        private void GridSpliteTo_ToRight(object sender, EventArgs e)
        {
            if (colRight.Width.Value == 0)
            {
                return;
            }

            if (colLeft.Width.Value == 0)
            {
                colLeft.Width = _lastColLeftLength;
                colRight.Width = _lastColRightLength;
            }
            else
            {
                _lastColLeftLength = colLeft.Width;
                _lastColRightLength = colRight.Width;

                colRight.Width = new GridLength(0);
                colLeft.Width = new GridLength(300, GridUnitType.Star);
            }
        }
        #endregion
        void Client_GetOrgForHRModelCompleted(object sender, EmpbasicService.GetOrgForHRModelCompletedEventArgs e)
        {
            foreach (EmpbasicService.OrgForHRModel i in e.Result)
            {
                forumList.Add(i);
            }
            //forumList.Add(new EmpbasicService.OrgForHRModel() { ID = 1, Type = "全部单位", Name = "组织机构" });
            //forumList.Add(new EmpbasicService.OrgForHRModel() { ID = 1, Type = "组织机构", Name = "党群系统" });
            //forumList.Add(new EmpbasicService.OrgForHRModel() { ID = 1, Type = "组织机构", Name = "政法系统" });

            //forumList.Add(new EmpbasicService.OrgForHRModel() { ID = 1, Type = "组织机构", Name = "行政系统" });
            //forumList.Add(new EmpbasicService.OrgForHRModel() { ID = 1, Type = "组织机构", Name = "参公系统" });
            //forumList.Add(new EmpbasicService.OrgForHRModel() { ID = 1, Type = "组织机构", Name = "地区系统" });
            AddTreeNode("全部单位", null);
        }
        ObservableCollection<EmpbasicService.OrgForHRModel> forumList = new ObservableCollection<EmpbasicService.OrgForHRModel>();


        private void AddTreeNode(string type, TreeViewItem treeViewItem)
        {
            List<EmpbasicService.OrgForHRModel> result = (from forumInfo in forumList
                                                          where forumInfo.Type == type
                                                          select forumInfo).ToList<EmpbasicService.OrgForHRModel>();

            if (result.Count > 0)
            {
                foreach (EmpbasicService.OrgForHRModel foruminfo in result)
                {
                    TreeViewItem objTreeNode = new TreeViewItem();

                    //StackPanel sp = new StackPanel();
                    CheckBox cb = new CheckBox();
                    cb.Content = foruminfo.Name;
                    cb.Tag = objTreeNode;
                    cb.DataContext = foruminfo;
                    //sp.Children.Add(cb);
                    cb.Click += new RoutedEventHandler(cb_Checked);
                    objTreeNode.Header = cb;

                    //objTreeNode.Header = foruminfo.Name;
                    //objTreeNode.DataContext = foruminfo;
                    //objTreeNode.Tag = foruminfo.ID;
                    if ((objTreeNode.Header as CheckBox).Content.ToString() == "组织机构") objTreeNode.IsExpanded = true;

                    //此样式将会添加的所有叶子结点上
                    //objTreeNode.ItemContainerStyle = this.Resources["RedItemStyle"] as Style;

                    //添加根节点
                    if (treeViewItem == null)
                    {
                        TreeOfLife.Items.Add(objTreeNode);
                    }
                    else
                    {
                        treeViewItem.Items.Add(objTreeNode);
                    }
                    AddTreeNode(foruminfo.Name, objTreeNode);
                }
            }
        }
        void cb_Checked(object sender, RoutedEventArgs e)
        {
            CheckBox cb = sender as CheckBox;
            EmpbasicService.OrgForHRModel rm = cb.DataContext as EmpbasicService.OrgForHRModel;
            if (cb.IsChecked == true)
            {
                if (!orgids.Contains(((EmpbasicService.OrgForHRModel)cb.DataContext).ID.ToString()))
                    orgids.Add(((EmpbasicService.OrgForHRModel)cb.DataContext).ID.ToString());
               
                CheckChildItem(cb.Tag as TreeViewItem);
            }
            else
            {
                if (orgids != null)
                {
                    if (orgids.Contains(((EmpbasicService.OrgForHRModel)cb.DataContext).ID.ToString()))
                        orgids.Remove(((EmpbasicService.OrgForHRModel)cb.DataContext).ID.ToString());
                }
                UnCheckChildItem(cb.Tag as TreeViewItem);
            }
            LoadData();
        }
        void CheckParentItem(TreeViewItem item)
        {
            TreeViewItem tvi = item.GetParentTreeViewItem();
            if (tvi != null)
            {
                CheckBox cb = tvi.Header as CheckBox;
                cb.IsChecked = true;
                CheckParentItem(tvi);
            }
        }
        public List<string> orgids = new List<string>();
        void CheckChildItem(TreeViewItem item)
        {
            foreach (TreeViewItem tvi in item.Items)
            {
                if (tvi != null)
                {
                    CheckBox cb = tvi.Header as CheckBox;
                    cb.IsChecked = true;
                    if (!orgids.Contains(((EmpbasicService.OrgForHRModel)cb.DataContext).ID.ToString()))
                        orgids.Add(((EmpbasicService.OrgForHRModel)cb.DataContext).ID.ToString());
                    CheckChildItem(tvi);
                }
            }
        }
        void UnCheckChildItem(TreeViewItem item)
        {
            foreach (TreeViewItem tvi in item.Items)
            {
                if (tvi != null)
                {
                    CheckBox cb = tvi.Header as CheckBox;
                    cb.IsChecked = false;
                    if (orgids.Contains(((EmpbasicService.OrgForHRModel)cb.DataContext).ID.ToString()))
                        orgids.Remove(((EmpbasicService.OrgForHRModel)cb.DataContext).ID.ToString());
                    UnCheckChildItem(tvi);
                }
            }
        }
        private void TreeOfLife_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            TreeViewItem item = e.NewValue as TreeViewItem;
            if (item != null)
            {
                if (item.Tag != null && Convert.ToInt32(item.Tag) != 0)
                {
                    OrgID = Convert.ToInt32(item.Tag);
                    LoadData();
                }
                
            }
        }

        void LoadData()
        {
            string or = "";
            if(orgids.Count > 0)
            {
                foreach(string i in orgids)
                {
                    or += i + ",";
                }
                or = or.Substring(0, or.Length - 1);
                Client.GetBianZhiModelCompleted += new EventHandler<EmpbasicService.GetBianZhiModelCompletedEventArgs>(Client_GetBianZhiModelCompleted);
                Client.GetBianZhiModelAsync(or, 0);
            }
            else DGRole.ItemsSource = null;
        }
        void Client_GetBianZhiModelCompleted(object sender, EmpbasicService.GetBianZhiModelCompletedEventArgs e)
        {
            BianZhiModels = e.Result;
            DGRole.ItemsSource = null;
            DGRole.ItemsSource = e.Result;
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void NewData_Click(object sender, RoutedEventArgs e)
        {
            if (orgids.Count != 1)
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择一个单位！");
                return;
            }
            BianZhiWindow_New RMWindow = new BianZhiWindow_New(Convert.ToInt32(orgids[0]), 0, true);
            RMWindow.Closed += new EventHandler(RMWindow_Closed);
            RMWindow.Show();
        }
        void RMWindow_Closed(object sender, EventArgs e)
        {
            //throw new NotImplementedException();
            LoadData();
        }

        private void EditData_Click(object sender, RoutedEventArgs e)
        {
            var seljw = this.DGRole.SelectedItem as EmpbasicService.BianZhiModel;

            //throw new NotImplementedException();
            if (seljw != null)
            {
                BianZhiWindow_New RMWindow = new BianZhiWindow_New(0, Convert.ToInt32(seljw.ID), false);
                RMWindow.Closed += new EventHandler(RMWindow_Closed);
                RMWindow.Show();

            }
            else SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择要编辑的编制！"); 
        }

        private void DeleteData_Click(object sender, RoutedEventArgs e)
        {
            var seljw = this.DGRole.SelectedItem as EmpbasicService.BianZhiModel;
            if (seljw != null)
            {
                if (MessageBox.Show("确定删除吗？", "提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                {
                    Client.DeleteBianZhiModelCompleted -= new EventHandler<EmpbasicService.DeleteBianZhiModelCompletedEventArgs>(Client_DeleteBianZhiModelCompleted);
                    Client.DeleteBianZhiModelCompleted += new EventHandler<EmpbasicService.DeleteBianZhiModelCompletedEventArgs>(Client_DeleteBianZhiModelCompleted);
                    Client.DeleteBianZhiModelAsync(seljw);
                }

            }
        }

        void Client_DeleteBianZhiModelCompleted(object sender, EmpbasicService.DeleteBianZhiModelCompletedEventArgs e)
        {
            SilverlightMessageBoxLibrary.Message.InfoMessage("删除成功！");
            LoadData();
        }

        private void SaveData_Click(object sender, RoutedEventArgs e)
        {
            if (orgids.Count != 1)
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择一个单位！");
                return;
            }
            BianZhiCheckWindow RMWindow = new BianZhiCheckWindow(Convert.ToInt32(orgids[0]), "乡科级正职核定数");
            RMWindow.Closed += new EventHandler(RMWindow_Closed);
            RMWindow.Show();
        }

        private void SaveData_Click1(object sender, RoutedEventArgs e)
        {
            if (MessageBox.Show("确定取消统筹吗？", "提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
            {
                if (orgids.Count != 1)
                {
                    SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择一个单位！");
                    return;
                }
                Client.QuXiaoTongChouAsync("乡科级正职核定数", Convert.ToInt32(orgids[0]));
            }
        }
        void Client_QuXiaoTongChouCompleted(object sender, EmpbasicService.QuXiaoTongChouCompletedEventArgs e)
        {
            SilverlightMessageBoxLibrary.Message.InfoMessage("取消统筹成功！");
            LoadData();
        }
        private void ViewData_Click(object sender, RoutedEventArgs e)
        {
            if (orgids.Count != 1)
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择一个单位！");
                return;
            }
            BianZhiCheckWindow RMWindow = new BianZhiCheckWindow(Convert.ToInt32(orgids[0]), "乡科级副职核定数");
            RMWindow.Closed += new EventHandler(RMWindow_Closed);
            RMWindow.Show();
        }

        private void ViewData_Click1(object sender, RoutedEventArgs e)
        {
            if (MessageBox.Show("确定取消统筹吗？", "提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
            {
                if (orgids.Count != 1)
                {
                    SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择一个单位！");
                    return;
                }
                Client.QuXiaoTongChouAsync("乡科级副职核定数", Convert.ToInt32(orgids[0]));
            }
        }
        private Stream ss = null;
        private void Export_Click(object sender, RoutedEventArgs e)
        {
            List<string> orgnames = new List<string>();
            if(BianZhiModels.Count == 0)
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("没有可以导出的数据！");
                return;
            }
            foreach(EmpbasicService.BianZhiModel b in BianZhiModels)
            {
                if (!orgnames.Contains(b.OrgName)) orgnames.Add(b.OrgName);
            }
            if(orgnames.Count > 1)
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("数据包含不同单位，只可导出同一单位的编制信息！");
                return;
            }
            ss = null;
            var downLoad = new SaveFileDialog()
            {
                Filter = "Excel files(2003) (*.xls)|*.xls",
            };
            if (downLoad.ShowDialog() == true)
            {
                ss = downLoad.OpenFile();
                PageInit();

            }
        }

        public void PageInit()
        {
            XlsDocument xls = new XlsDocument();
            XF xf = xls.NewXF();
            xf.HorizontalAlignment = HorizontalAlignments.Centered;
            xf.VerticalAlignment = VerticalAlignments.Centered;
            xf.UseBorder = true;
            xf.TopLineStyle = 1;
            xf.TopLineColor = org.in2bits.MyXls.Colors.Black;
            xf.BottomLineStyle = 1;
            xf.BottomLineColor = org.in2bits.MyXls.Colors.Black;
            xf.LeftLineStyle = 1;
            xf.LeftLineColor = org.in2bits.MyXls.Colors.Black;
            xf.RightLineStyle = 1;
            xf.RightLineColor = org.in2bits.MyXls.Colors.Black;

            XF xf1 = xls.NewXF();
            xf1.UseBorder = true;
            xf1.TopLineStyle = 1;
            xf1.TopLineColor = org.in2bits.MyXls.Colors.Black;
            xf1.BottomLineStyle = 1;
            xf1.BottomLineColor = org.in2bits.MyXls.Colors.Black;
            xf1.LeftLineStyle = 1;
            xf1.LeftLineColor = org.in2bits.MyXls.Colors.Black;
            xf1.RightLineStyle = 1;
            xf1.RightLineColor = org.in2bits.MyXls.Colors.Black;
            xf1.HorizontalAlignment = HorizontalAlignments.Centered;
            xf1.VerticalAlignment = VerticalAlignments.Centered;
            xf1.Font.Bold = true;
            xf1.Font.Height = 15 * 20;

            XF xf3 = xls.NewXF();
            xf3.UseBorder = true;
            xf3.TopLineStyle = 1;
            xf3.TopLineColor = org.in2bits.MyXls.Colors.Black;
            xf3.BottomLineStyle = 1;
            xf3.BottomLineColor = org.in2bits.MyXls.Colors.Black;
            xf3.LeftLineStyle = 1;
            xf3.LeftLineColor = org.in2bits.MyXls.Colors.Black;
            xf3.RightLineStyle = 1;
            xf3.RightLineColor = org.in2bits.MyXls.Colors.Black;
            xf3.HorizontalAlignment = HorizontalAlignments.Centered;
            xf3.VerticalAlignment = VerticalAlignments.Centered;
            xf3.Font.Bold = true;
            xf3.Font.Height = 12 * 20;

            Worksheet sheet1 = xls.Workbook.Worksheets.AddNamed("徐汇区科以下公务员队伍结构情况分析表");
            sheet1.Cells.Add(6, 1, "", xf3);
            sheet1.Cells.Add(6, 2, "", xf3);
            sheet1.Cells.Add(6, 3, "", xf3);
            sheet1.Cells.Add(6, 4, "", xf3);
            sheet1.Cells.Add(6, 5, "", xf3);
            sheet1.Cells.Add(6, 6, "", xf3);
            sheet1.Cells.Add(6, 7, "", xf3);
            sheet1.Cells.Add(6, 8, "", xf3);
            sheet1.Cells.Add(6, 9, "", xf3);
            sheet1.Cells.Add(6, 10, "", xf3);
            sheet1.Cells.Merge(1, 2, 1, 18);
            sheet1.Cells.Add(1, 1, DateTime.Today.Year + "年徐汇区科以下公务员队伍结构情况分析表", xf1);

            sheet1.Cells.Add(4, 1, "单\n位\n名\n称", xf3);
            sheet1.Cells.Merge(4, 6, 1, 1);
            sheet1.Cells.Add(4, 2, "编制数", xf3);
            sheet1.Cells.Add(4, 3, "", xf3);
            sheet1.Cells.Add(4, 4, "", xf3);
            sheet1.Cells.Merge(4, 4, 2, 4);
            sheet1.Cells.Add(4, 5, "实有人数", xf3);
            sheet1.Cells.Add(4, 6, "", xf3);
            sheet1.Cells.Add(4, 7, "", xf3);
            sheet1.Cells.Add(4, 8, "", xf3);
            sheet1.Cells.Add(4, 9, "", xf3);
            sheet1.Cells.Merge(4, 4, 5, 9);
            sheet1.Cells.Add(4, 10, "所在科室", xf3);
            sheet1.Cells.Merge(4, 6, 10, 10);
            sheet1.Cells.Add(4, 11, "职数配备情况", xf3);
            sheet1.Cells.Add(4, 12, "", xf3);
            sheet1.Cells.Add(4, 13, "", xf3);
            sheet1.Cells.Add(4, 14, "", xf3);
            sheet1.Cells.Add(4, 15, "", xf3);
            sheet1.Cells.Add(4, 16, "", xf3);
            sheet1.Cells.Add(4, 17, "", xf3);
            sheet1.Cells.Add(4, 18, "", xf3);
            sheet1.Cells.Merge(4, 4, 11, 18);

            sheet1.Cells.Add(5, 11, "领导职务", xf3);
            sheet1.Cells.Add(5, 12, "", xf3);
            sheet1.Cells.Add(5, 13, "", xf3);
            sheet1.Cells.Add(5, 14, "", xf3);
            sheet1.Cells.Merge(5, 5, 11, 14);
            sheet1.Cells.Add(5, 15, "非领导职务", xf3);
            sheet1.Cells.Add(5, 16, "", xf3);
            sheet1.Cells.Add(5, 17, "", xf3);
            sheet1.Cells.Add(5, 18, "", xf3);
            sheet1.Cells.Merge(5, 5, 15, 18);

            sheet1.Cells.Add(5, 2, "机关行政编制数", xf3);
            sheet1.Cells.Merge(5, 6, 2, 2);
            sheet1.Cells.Add(5, 3, "政法行政编制数", xf3);
            sheet1.Cells.Merge(5, 6, 3, 3);
            sheet1.Cells.Add(5, 4, "参照管理事业编制数", xf3);
            sheet1.Cells.Merge(5, 6, 4, 4);
            sheet1.Cells.Add(5, 5, "总数", xf3);
            sheet1.Cells.Merge(5, 6, 5, 5);
            sheet1.Cells.Add(5, 6, "局级、县处级(正、副)实有人数", xf3);
            sheet1.Cells.Merge(5, 6, 6, 6);
            sheet1.Cells.Add(5, 7, "乡科级 （正、副）实有人数", xf3);
            sheet1.Cells.Merge(5, 6, 7, 7);
            sheet1.Cells.Add(5, 8, "科员级实有人数", xf3);
            sheet1.Cells.Merge(5, 6, 8, 8);
            sheet1.Cells.Add(5, 9, "试用期内或未过渡人员", xf3);
            sheet1.Cells.Merge(5, 6, 9, 9);

            sheet1.Cells.Add(6, 11, "乡科级正职职数", xf3);
            sheet1.Cells.Add(6, 12, "乡科级正职实有人数", xf3);
            sheet1.Cells.Add(6, 13, "乡科级副职职数", xf3);
            sheet1.Cells.Add(6, 14, "乡科级副职实有人数", xf3);
            sheet1.Cells.Add(6, 15, "主任科员职数", xf3);
            sheet1.Cells.Add(6, 16, "主任科员实有人数", xf3);
            sheet1.Cells.Add(6, 17, "副主任科员职数", xf3);
            sheet1.Cells.Add(6, 18, "副主任科员实有人数", xf3);

            ColumnInfo colInfoxueli1 = new ColumnInfo(xls, sheet1);
            colInfoxueli1.Width = 40 * 256;
            colInfoxueli1.ColumnIndexStart = 0;
            colInfoxueli1.ColumnIndexEnd = 0;
            sheet1.AddColumnInfo(colInfoxueli1);

            ColumnInfo colInfoxueli2 = new ColumnInfo(xls, sheet1);
            colInfoxueli2.Width = 20 * 256;
            colInfoxueli2.ColumnIndexStart = 1;
            colInfoxueli2.ColumnIndexEnd = 1;
            sheet1.AddColumnInfo(colInfoxueli2);

            ColumnInfo colInfoxueli3 = new ColumnInfo(xls, sheet1);
            colInfoxueli3.Width = 20 * 256;
            colInfoxueli3.ColumnIndexStart = 2;
            colInfoxueli3.ColumnIndexEnd = 2;
            sheet1.AddColumnInfo(colInfoxueli3);

            ColumnInfo colInfoxueli4 = new ColumnInfo(xls, sheet1);
            colInfoxueli4.Width = 20 * 256;
            colInfoxueli4.ColumnIndexStart = 3;
            colInfoxueli4.ColumnIndexEnd = 3;
            sheet1.AddColumnInfo(colInfoxueli4);


            for (int i = 0; i < BianZhiModels.Count; i++)
            {
                if (BianZhiModels[i].OrgName != null) sheet1.Cells.Add(i + 7, 1, BianZhiModels[i].OrgName.ToString(), xf);
                else sheet1.Cells.Add(i + 7, 1, "", xf);
                if (BianZhiModels[i].JiGuanXingZheng != null) sheet1.Cells.Add(i + 7, 2, BianZhiModels[i].JiGuanXingZheng.ToString(), xf);
                else sheet1.Cells.Add(i + 7, 2, "", xf);
                if (BianZhiModels[i].ZhengFaXingZheng != null) sheet1.Cells.Add(i + 7, 3, BianZhiModels[i].ZhengFaXingZheng.ToString(), xf);
                else sheet1.Cells.Add(i + 7, 3, "", xf);
                if (BianZhiModels[i].CanZhaoGuanLi != null) sheet1.Cells.Add(i + 7, 4, BianZhiModels[i].CanZhaoGuanLi.ToString(), xf);
                else sheet1.Cells.Add(i + 7, 4, "", xf);

                if (BianZhiModels[i].ZongShu != null) sheet1.Cells.Add(i + 7, 5, BianZhiModels[i].ZongShu.ToString(), xf);
                else sheet1.Cells.Add(i + 7, 5, "", xf);
                if (BianZhiModels[i].ChuJiRenShu != null) sheet1.Cells.Add(i + 7, 6, BianZhiModels[i].ChuJiRenShu.ToString(), xf);
                else sheet1.Cells.Add(i + 7, 6, "", xf);
                if (BianZhiModels[i].XiangKeZhengFuZ != null) sheet1.Cells.Add(i + 7, 7, BianZhiModels[i].XiangKeZhengFuZ.ToString(), xf);
                else sheet1.Cells.Add(i + 7, 7, "", xf);
                if (BianZhiModels[i].KeYuanShiYou != null) sheet1.Cells.Add(i + 7, 8, BianZhiModels[i].KeYuanShiYou.ToString(), xf);
                else sheet1.Cells.Add(i + 7, 8, "", xf);
                if (BianZhiModels[i].WeiDingZhi != null) sheet1.Cells.Add(i + 7, 9, BianZhiModels[i].WeiDingZhi.ToString(), xf);
                else sheet1.Cells.Add(i + 7, 9, "", xf);

                if (BianZhiModels[i].JiGouMingCheng != null) sheet1.Cells.Add(i + 7, 10, BianZhiModels[i].JiGouMingCheng.ToString(), xf);
                else sheet1.Cells.Add(i + 7, 10, "", xf);
                if (BianZhiModels[i].ZhengKeLingDao != null) sheet1.Cells.Add(i + 7, 11, BianZhiModels[i].ZhengKeLingDao.ToString(), xf);
                else sheet1.Cells.Add(i + 7, 11, "", xf);
                if (BianZhiModels[i].ZhengKeLingDaoShiYou != null) sheet1.Cells.Add(i + 7, 12, BianZhiModels[i].ZhengKeLingDaoShiYou.ToString(), xf);
                else sheet1.Cells.Add(i + 7, 12, "", xf);
                if (BianZhiModels[i].ZhengKeFeiLingDao != null) sheet1.Cells.Add(i + 7, 13, BianZhiModels[i].ZhengKeFeiLingDao.ToString(), xf);
                else sheet1.Cells.Add(i + 7, 13, "", xf);

                if (BianZhiModels[i].FuKeLingDaoShiYou != null) sheet1.Cells.Add(i + 7, 14, BianZhiModels[i].FuKeLingDaoShiYou.ToString(), xf);
                else sheet1.Cells.Add(i + 7, 14, "", xf);
                if (BianZhiModels[i].FuKeLingDao != null) sheet1.Cells.Add(i + 7, 15, BianZhiModels[i].FuKeLingDao.ToString(), xf);
                else sheet1.Cells.Add(i + 7, 15, "", xf);
                if (BianZhiModels[i].ZhengKeFeiLingDaoShiYou != null) sheet1.Cells.Add(i + 7, 16, BianZhiModels[i].ZhengKeFeiLingDaoShiYou.ToString(), xf);
                else sheet1.Cells.Add(i + 7, 16, "", xf);
                if (BianZhiModels[i].FuKeFeiLingDao != null) sheet1.Cells.Add(i + 7, 17, BianZhiModels[i].FuKeFeiLingDao.ToString(), xf);
                else sheet1.Cells.Add(i + 7, 17, "", xf);
                if (BianZhiModels[i].FuKeFeiLingDaoShiYou != null) sheet1.Cells.Add(i + 7, 18, BianZhiModels[i].FuKeFeiLingDaoShiYou.ToString(), xf);
                else sheet1.Cells.Add(i + 7, 18, "", xf);
            }

            sheet1.Cells.Merge(7, BianZhiModels.Count + 6, 1, 1);
            sheet1.Cells.Merge(7, BianZhiModels.Count + 6, 2, 2);
            sheet1.Cells.Merge(7, BianZhiModels.Count + 6, 3, 3);
            sheet1.Cells.Merge(7, BianZhiModels.Count + 6, 4, 4);
            sheet1.Cells.Merge(7, BianZhiModels.Count + 6, 5, 5);
            sheet1.Cells.Merge(7, BianZhiModels.Count + 6, 6, 6);
            sheet1.Cells.Merge(7, BianZhiModels.Count + 6, 7, 7);
            sheet1.Cells.Merge(7, BianZhiModels.Count + 6, 8, 8);
            sheet1.Cells.Merge(7, BianZhiModels.Count + 6, 9, 9);

            sheet1.Cells.Merge(7, BianZhiModels.Count + 6, 15, 15);
            sheet1.Cells.Merge(7, BianZhiModels.Count + 6, 17, 17);

            int sint = 7;
            int eint = 7;
            for (int i = 0; i < BianZhiModels.Count - 1; i++)
            {
                
                    if (BianZhiModels[i].ZhengKeLingDaoTC == true && BianZhiModels[i + 1].ZhengKeLingDaoTC == true && BianZhiModels[i].TCMC == BianZhiModels[i + 1].TCMC)
                    {
                        eint += 1;

                    }
                    else
                    {
                        sheet1.Cells.Merge(sint, eint, 11, 11);
                        sint = eint = eint + 1;
                    }
            }

            int sint1 = 7;
            int eint1 = 7;
            for (int i = 0; i < BianZhiModels.Count - 1; i++)
            {
                if (BianZhiModels[i].ZhengKeFeiLingDaoTC == true && BianZhiModels[i + 1].ZhengKeFeiLingDaoTC == true && BianZhiModels[i].TCMC1 == BianZhiModels[i + 1].TCMC1)
                {
                    eint1 += 1;
                }
                else
                {
                    sheet1.Cells.Merge(sint1, eint1, 13, 13);
                    sint1 = eint1 = eint1 + 1;
                }
            }
            xls.Save(ss);
            ss.Close();
            ss.Dispose();
            SilverlightMessageBoxLibrary.Message.InfoMessage("导出成功！");
        }
    }
}
